<?php

include_once('user_controller.php');

class Person extends User_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('person_model');
        $this->load->library('session');
        $this->load->helper('address');
        $this->load->helper('user');
        $this->load->helper('person_display');
	}

	public function display($id, $roll)
	{
		$person = $this->person_model->get ($id);

        $canvass_date = $this->session->userdata('canvass_date');
		
        if (isset($canvass_date) && strlen ($canvass_date) > 0)
        {
//            echo ("canvass_date set: $canvass_date");
            $canvasser = $this->session->userdata('canvasser');
        }
        else
        {
//            echo ("canvass_date not set");
            // seems to be the first time in here, so set defaults for both
//            $canvass_date = strftime ("%d/%m/%y", time());   
            $canvass_date = strftime ("%d %b %y", time());   
            $canvasser = get_current_username();         
        }
        
        $data['roll'] = $roll;
        
		$data['title'] = $person->surname . ", " . $person->first_names;
		$data['person'] = $person;

        $data['addresses'] = $this->person_model->get_addresses ($id);
        $data['voting_record'] = $this->person_model->get_voting_record ($id);
        $data['contacts'] = $this->person_model->get_contacts ($id);
        $data['aliases'] = $this->person_model->get_aliases ($id);

        $data['canvass_date'] = $canvass_date;
        $data['canvasser'] = $canvasser;
        
		$data['content'] = 'person_display';
		$this->load->view('layout/main', $data);
	}

    function add_canvassing_notes()
    {
        $this->load->helper('form');
        $this->load->helper('url');
        
        $id = $this->input->post('id');
        $roll = $this->input->post('roll');
        
//        echo ("add_canvassing_notes. id(1): " . $id);
        
        $date = $this->input->post('date');
        $canvasser = $this->input->post('canvasser');
        $party = $this->input->post('party');
        
        $rank = $this->input->post('rank');
        
        if ($rank == -1)
        {
            $rank = NULL;
        }
        $notes = $this->input->post('notes');
        
        $this->session->set_userdata('canvass_date', $date);
        $this->session->set_userdata('canvasser', $canvasser);
        
        $model_date = strftime ("%Y-%m-%d", strtotime ($date));
        
        $this->person_model->add_contact_details ($id, $model_date, $canvasser, $party, $rank, $notes);
        
//        echo ("add_canvassing_notes. id(2): " . $id);

        redirect ("person/display/$id/$roll");
    }
}
?>